package springmvcdemo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import springmvcdemo.model.Student;

public class JsonDemo {
    private static ObjectMapper objectMapper = new ObjectMapper();//用来将对象转成json数据的对象
    public static void main(String[] args) throws JsonProcessingException {
        //1.有一个对象
        Student student = new Student(180,"男","张三",18);
        //2.转成json
        String json = objectMapper.writeValueAsString(student);
        System.out.println(json);

        //json转对象
        //1.定义一个json数据
        String json2 = "  {\"name\":\"张三\",\"age\":18,\"sex\":\"男\",\"height\":180.0}  ";
        //2.定义对象并转化
        Student student2 = objectMapper.readValue(json2,Student.class);
        System.out.println(student2);

    }
}
